package com.ky.pro.java.mq;

import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.admin.NewTopic;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.config.TopicBuilder;
import org.springframework.kafka.core.KafkaTemplate;

@Slf4j
@SpringBootApplication
public class KafkaDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(KafkaDemoApplication.class, args);
    }


    @Bean
    public NewTopic topic() {
        return TopicBuilder.name("ky_test")
                .partitions(3)
                .replicas(1)
                .build();
    }

    @KafkaListener(id = "ky-001", topics = "ky_test")
    public void listen(String msg) {
        log.info("接收到消息：" + msg);
    }

    @Bean
    public ApplicationRunner runner(KafkaTemplate<String, String> template) {
        return args -> template.send("ky_test", "test msg from producer");
    }
}
